package com.yycx.base.provider.controller;

import cn.hutool.core.map.MapUtil;
import com.yycx.base.provider.handler.BaseAnalysisHandler;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 首页统计
 *
 * @author flyme
 * @date 2022-03-01
 */
@RestController
@RequestMapping("/analysis/")
public class AnalysisController {

    @Autowired(required = false)
    private  BaseAnalysisHandler analysisHandler;

    @ApiOperation(value = "首页卡片统计")
    @GetMapping(value = "growCard")
    public ResultBody growCard(@RequestParam(required = false) Map params) {
        if (FlymeUtils.isNotEmpty(analysisHandler)) {
            List<EntityMap> growCardList = analysisHandler.getGrowCard(params);
            return ResultBody.ok(growCardList);
        }
        return ResultBody.ok();
    }

    @ApiOperation(value = "柱形图")
    @GetMapping(value = "visitAnalysisBar")
    public ResultBody visitAnalysisBar(@RequestParam(required = false) Map params) {
        if (FlymeUtils.isNotEmpty(analysisHandler)) {
            String dateRuleCode = params.getOrDefault("dateRuleCode", "year").toString();
            if (FlymeUtils.isEmpty(dateRuleCode)) {
                ApiAssert.failure("缺少日期格式参数");
            }
            List<EntityMap> list = analysisHandler.visitAnalysisBar(params, dateRuleCode);
            EntityMap result = new EntityMap();
            switch (dateRuleCode) {
                case "year":
                    result = getVisitAnalysisBarDataByYear(list);
                    break;
                case "month":
                    result = getVisitAnalysisBarDataByMonth(list);
                    break;
                case "week":
                    result = getVisitAnalysisBarDataByWeek(list);
                    break;
                case "day":
                    result = getVisitAnalysisBarDataByDay(list);
            }
            return ResultBody.ok(result);
        }
        return ResultBody.ok();
    }

    @ApiOperation(value = "排名图")
    @GetMapping(value = "visitAnalysisTopBar")
    public ResultBody visitAnalysisTopBar(@RequestParam(required = false) Map params) {
        if (FlymeUtils.isNotEmpty(analysisHandler)) {
            String dateRuleCode = params.getOrDefault("dateRuleCode", "year").toString();
            if (FlymeUtils.isEmpty(dateRuleCode)) {
                ApiAssert.failure("缺少日期格式参数");
            }
            EntityMap map = analysisHandler.visitAnalysisTopBar(params);
            return ResultBody.ok(map);
        }
        return ResultBody.ok();
    }


    /**
     * 按年统计成交额
     *
     * @param list
     * @return
     */
    private EntityMap getVisitAnalysisBarDataByYear(List<EntityMap> list) {
        EntityMap result = new EntityMap();
        List<String> xData = new ArrayList<>();
        List<Object> yData = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            xData.add(i + "月");
            yData.add(0);
            if (FlymeUtils.isNotEmpty(list)) {
                for (Map<String, Object> map : list) {
                    String totalAmount = map.get("value").toString();
                    int date = FlymeUtils.getInteger(map.get("date").toString(), 0);
                    if (date == i) {
                        yData.set(i - 1, new BigDecimal(totalAmount));
                    }
                }
            }
        }
        result.put("xData", xData);
        result.put("yData", yData);
        return result;

    }

    /**
     * 按月统计成交额
     *
     * @param list
     * @return
     */
    private EntityMap getVisitAnalysisBarDataByMonth(List<EntityMap> list) {
        EntityMap result = new EntityMap();
        List<String> xData = new ArrayList<>();
        List<Object> yData = new ArrayList<>();
        int num = DateUtils.getMonthDaysNum();

        for (int i = 0; i < num; i++) {
            String date = DateUtils.plusDays(DateUtils.getFirstDayOfMonth(), i);
            xData.add(date);
            yData.add(0);
            if (FlymeUtils.isNotEmpty(list)) {
                for (Map<String, Object> map : list) {
                    String totalAmount = map.get("value").toString();
                    String d = MapUtil.getStr(map, "date");
                    if (date.equals(d)) {
                        yData.set(i, new BigDecimal(totalAmount));
                    }
                }
            }
        }
        result.put("xData", xData);
        result.put("yData", yData);
        return result;

    }

    /**
     * 按周统计成交额
     *
     * @param list
     * @return
     */
    private EntityMap getVisitAnalysisBarDataByWeek(List<EntityMap> list) {
        EntityMap result = new EntityMap();
        List<String> xData = new ArrayList<>();
        List<Object> yData = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            String date = DateUtils.getDayOfWeek(DateUtils.plusDays(DateUtils.getFirstDayOfWeek(), i));
            xData.add(date);
            yData.add(0);
            if (FlymeUtils.isNotEmpty(list)) {
                for (Map<String, Object> map : list) {
                    String totalAmount = map.get("value").toString();
                    String d = MapUtil.getStr(map, "date");
                    if (date.equals(d)) {
                        yData.set(i - 1, new BigDecimal(totalAmount));
                    }
                }
            }
        }
        result.put("xData", xData);
        result.put("yData", yData);
        return result;

    }

    /**
     * 按天统计成交额
     *
     * @param list
     * @return
     */
    private EntityMap getVisitAnalysisBarDataByDay(List<EntityMap> list) {
        EntityMap result = new EntityMap();
        List<String> xData = new ArrayList<>();
        List<Object> yData = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            String date = i + "点";
            xData.add(date);
            yData.add(0);
            if (FlymeUtils.isNotEmpty(list)) {
                for (Map<String, Object> map : list) {
                    String totalAmount = map.get("value").toString();
                    int d = MapUtil.getInt(map, "date");
                    if (i == d) {
                        yData.set(i, new BigDecimal(totalAmount));
                    }
                }
            }
        }
        result.put("xData", xData);
        result.put("yData", yData);
        return result;

    }


}
